package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Menu;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * ClassName: MenuServiceImpl
 * Description:
 * date: 2019/12/1 11:25
 * Author Airson
 * Since JDK 1.8
 */
@Service
public class MenuServiceImpl implements MenuService {

    @Autowired
    MenuDao menuDao;

    @Override
    public void add(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        //根据父菜单id,查询path并查询
        String parentPath = menuDao.findparentPathById(parentMenuId);
        //查询父类id为parid的个数
        Integer menuCount = menuDao.findmenuCountById(parentMenuId)+1;

        String menuPath = "/" + parentPath + "-" + menuCount;
        menu.setPath(menuPath);


        menuDao.add(menu);
    }

    @Override
    public PageResult queryPage(QueryPageBean queryPageBean) {
        //开始分页
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //条件查询
        Page<Menu> page = menuDao.findByCondition(queryPageBean.getQueryString());

        //返回结果
        return new PageResult(page.getTotal(), page);
    }

    @Override
    public void delById(Integer id) {
        //1.判断检查项是否被检查组关联，如果关联，提示不能删除
        //查询检查项id是否在中间表出现的次数
        long count = menuDao.findCountById(id);
        if (count > 0) {
            throw new RuntimeException("该菜单被关联，不能删除！！！");
        } else {
            menuDao.delById(id);
        }
    }

    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    @Override
    public void edit(Menu menu) {
        Integer parentMenuId = menu.getParentMenuId();
        //根据父菜单id,查询path并查询
        String parentPath = menuDao.findparentPathById(parentMenuId);
        //查询父类id为parid的个数
        Integer menuCount = menuDao.findmenuCountById(parentMenuId)+1;

        String menuPath = "/" + parentPath + "-" + menuCount;
        menu.setPath(menuPath);
        menuDao.edit(menu);

    }

    @Override
    public List<Menu> findAll() {
        return menuDao.findAll();
    }
}
